package com.own.business.system.user.business.util;

import com.own.business.system.user.business.core.AfterRegisterUserSpringHook;
import com.own.business.system.user.common.constant.ConstantSystemUser;
import com.own.business.system.user.entity.po.SystemUser;
import com.own.business.system.user.mapper.SystemUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.function.Consumer;

/**
 * CreateUserUtil
 *
 * @author chenxueli
 * @date 2023-06-30 11:50:00
 */
@Slf4j
@Component
public class CreateUserUtil {

    @Resource
    private SystemUserMapper systemUserMapper;

    @Resource
    private AfterRegisterUserSpringHook afterRegisterUserSpringHook;

    /**
     * 创建一个新的用户
     *
     * @param consumer 用户信息的处理函数
     * @return 用户信息
     */
    public SystemUser createUser(
            Consumer<SystemUser> consumer
    ) {
        // 创建一个新的账号
        var user = new SystemUser()
                .setFromType(ConstantSystemUser.FromType.UNKNOWN)
                .setState(ConstantSystemUser.State.NORMAL)
                .setIsDisable(0);
        consumer.accept(user);
        // 添加账号信息
        systemUserMapper.insert(user);
        // 执行注册后的钩子
        afterRegisterUserSpringHook.afterRegisterUser(user);
        return user;
    }

}
